<?php
/***************************************************************
*  DYNAMO - Simple yet powerful CMS - www.dynamocms.com
*  Copyright (c) M31 2003-2008 - www.m31web.com
*
*  This script is part of the DYNAMO project. The DYNAMO
*  project is free software; you can redistribute it and/or modify
*  it under the terms of the GNU General Public License as published by
*  the Free Software Foundation; either version 2 of the License, or
*  (at your option) any later version.
*
*  The GNU General Public License can be found at
*  http://www.gnu.org/copyleft/gpl.html or included in the distribution
*
*  This script is distributed in the hope that it will be useful,
*  but WITHOUT ANY WARRANTY; without even the implied warranty of
*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
*  GNU General Public License for more details.
*
*  This copyright notice MUST APPEAR in all copies of the script!
*
*  File Name: xxx.php
*      XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
* 	
*  File Authors:
*      1.0 : Creation by Jean-Francois Faudi
* 
***************************************************************/

	// taille des champs de texte
	if ($tsize == 0)
		$tsize = 20;
	
	// conversion du texte
	function text2html($string)
	{
		$string = stripslashes($string);
		$string = ereg_replace(39, 146, $string);
		//$string = ereg_replace(10, "<br>", $string);
		$string = ereg_replace(13, "", $string);
		return $string;
	}

	if ($mode == "edit")
	{
		for ($index = 0; $index < $index_max; $index++)
		{
			$key = "what_" . $index;
			if ($$key == "modify")
			{
				// teste si l'identifiant interne est nul
				$cur_id = "id_" . $index;
				if ($$cur_id == 0)
				{
					$max = 1000;
					$query = "SELECT MAX(id) AS max FROM " . $table_name;
					$mysql_result = database_query($query);
					$row = mysql_fetch_object($mysql_result);
					if ($row->max > $max)
						$max = $row->max;
					$$cur_id = $max + 1;

					// insertion du nouvel enregistrement
					$query = "INSERT INTO " . $table_name . " (id) VALUES (" . $$cur_id . ")"; 
					$mysql_result = database_query($query);
				}

				$query = "UPDATE " . $table_name . " SET ";
				
				reset($fieldlist);
				while (list($key, $name) = each($fieldlist))
				{
					if ($key == "id")
						continue;
						
					$input = $key . "_" . $index;

					// formattage des dates
					if ((is_array($name)) and ($name["type"] == "date"))
					{
						$dummy = explode("/", $$input);
						$date = mktime(0, 0, 0, $dummy[1], $dummy[0], $dummy[2]);
						$query .= $key . "=" . $date . ", ";
					}
					else
						$query .= $key . "='" . text2html($$input) . "', ";
				}

				$query = substr($query, 0, -2);
				$query .= " WHERE id = " . $$cur_id;  
				$mysql_result = database_query($query);
			}
		}
	}

	if ($message) 
		print "<p class=\"page_warning\">$message</p>\n"; 
	else
		print "<br>\n";

	if ($mode == "edit")
		print "<form name=\"myform\" method=\"post\" action=\"" . $PHP_SELF . "\">\n";

	// calcul du nombre d'enregistrement
	$query = "SELECT COUNT(*) AS count FROM " . $table_name . $table_criteria;
	$mysql_result = database_query($query);
	$row = mysql_fetch_array($mysql_result);
	$total_records = $row["count"];
	$start = intval($start);
	
	if ($total_records > 30)
	{
		print "<center><span class=\"small_text\" >";
		for ($i = 0; $i < $total_records / 30; $i++)
		{
			if ($start != ($i*30))
				print "<a href=\"" . $PHP_SELF . "?mode=" . $mode . "&orderby=" . $orderby . "&start=" . $i*30 . "\">" . ($i+1) . "</a>&nbsp;";
			else
				print ($i+1) . "&nbsp;";
		}
		print "</span></center>";
		print "<br>";
	}
	
	print "<table border=\"0\" cellspacing=\"0\" cellpadding=\"0\" align=\"center\">\n";

  	print "<tr>\n";
	if (($mode != "edit") && (file_exists("display.php")))
		print "<td>&nbsp;</td><td>&nbsp;</td>\n";

	reset($fieldlist);
	while (list($key, $name) = each($fieldlist))
	{
		if (is_array($name))
			print "<td class=\"table_column_header\" bgcolor=\"#FFFFFF\"><a href=" . $PHP_SELF . "?orderby="  . $key . "&mode=" . $mode . ">" . $name["title"] . "</a>&nbsp;</td>\n";
		else if ($name != "")
			print "<td class=\"table_column_header\" bgcolor=\"#FFFFFF\"><a href=" . $PHP_SELF . "?orderby="  . $key . "&mode=" . $mode . ">" . $name . "</a>&nbsp;</td>\n";
	}
	if ($mode != "edit")
		print "<td>&nbsp;</td>\n";
	print "</tr>\n";
	
	if ($orderby)    
		$query = "SELECT * FROM " . $table_name . $table_criteria . " ORDER BY " . $orderby . " LIMIT " . $start . ", 30";
	else
		$query = "SELECT * FROM " . $table_name . $table_criteria . " LIMIT " . $start . ", 30";
	
	$mysql_result = database_query($query);
		
	$index = 0;
	while($row = mysql_fetch_array($mysql_result))
	{			
		print "<tr>";
		
		if ($mode == "edit")
		{
			print "<input type=\"hidden\" name=\"what_" . $index . "\" value=\"nothing\">\n";

			reset($fieldlist);
			while (list($key, $name) = each($fieldlist))
			{
				if (is_array($name))
				{
					if ($name["type"] == "select")
					{
						print "<td class=\"small_text\"><select class=\"small_text\" name=\"" . $key . "_" . $index . "\" onChange=\"document.myform.what_" . $index . ".value='modify'\">\n";
						while (list($option_id, $option_title) = each($name["options"]))
						{
							$is_selected = "";
							if ($row[$key] == $option_id)
								$is_selected = " selected";
							print "<option value=\"" . $option_id . "\"" . $is_selected . ">" . $option_title . "</option>\n";
						}
						print "</select></td>";
					}
					else if ($name["type"] == "check")
					{
						$is_selected = "";
						if ($row[$key] == 1)
							$is_selected = " checked";
						print "<td class=\"small_text\"><input type=\"checkbox\" class=\"small_text\" name=\"" . $key . "_" . $index . "\" value=\"1\" onChange=\"document.myform.what_" . $index . ".value='modify'\"" . $is_selected . ">\n";
					}
					else if ($name["type"] == "date")
						print "<td class=\"small_text\"><input class=\"small_text\" type=\"text\" size=\"" . $tsize . "\" name=\"" . $key . "_" . $index . "\" value=\"" . date("d/m/Y", $row[$key]) . "\" onChange=\"document.myform.what_" . $index . ".value='modify'\"></td>\n";
					else if (($name["type"] == "text") or ($name["type"] == "comment"))
						print "<td class=\"small_text\"><input class=\"small_text\" type=\"text\" size=\"" . $tsize . "\" name=\"" . $key . "_" . $index . "\" value=\"" . $row[$key] . "\" onChange=\"document.myform.what_" . $index . ".value='modify'\"></td>\n";
				}
				else if ($name != "")
					print "<td class=\"small_text\"><input class=\"small_text\" type=\"text\" size=\"" . $tsize . "\" name=\"" . $key . "_" . $index . "\" value=\"" . $row[$key] . "\" onChange=\"document.myform.what_" . $index . ".value='modify'\"></td>\n";
				else
					print "<input type=\"hidden\" name=\"" . $key . "_" . $index . "\" value=\"" . $row[$key] . "\">\n";
			}
		}
		else
		{
			if (file_exists("display.php"))
			{
				print "<td class=\"small_text\"><a href=\"display.php?orderby=" . $orderby . "&id=" . $row["id"] . "\"><img src=\"../../cms/images/view.gif\" border=\"0\"></a></td>\n";
				print "<td class=\"small_text\"><a href=\"display.php?mode=edit&orderby=" . $orderby . "&id=" . $row["id"] . "\"><img src=\"../../cms/images/edit.gif\" border=\"0\"></a></td>\n";
			}

			reset($fieldlist);
			while (list($key, $name) = each($fieldlist))
			{
				if (is_array($name))
				{
					if (($name["type"] == "select") or ($name["type"] == "check"))
						print "<td class=\"small_text\">" . $name["options"][$row[$key]] . "&nbsp;</td>\n";
					else if ($name["type"] == "date")
						print "<td class=\"small_text\">" . date("d/m/Y", $row[$key]) . "&nbsp;</td>\n";
				}
				else if ($name != "")
					print "<td class=\"small_text\">" . $row[$key] . "&nbsp;</td>\n";
			}
			print "<td class=\"small_text\"><a href=\"" . $PHP_SELF . "?what=delete&id=" . $row["id"] . "\"><img border=\"0\" src=\"../../cms/images/waste.gif\"></a></td>\n";
		}
		
		$index++;
		print "</tr>\n";
	}
		
	if ($mode == "edit")
	{
		print "<tr>";
		print "<input type=\"hidden\" name=\"what_" . $index . "\" value=\"nothing\">\n";

		reset($fieldlist);
		while (list($key, $name) = each($fieldlist))
		{
			if (is_array($name))
			{
				if ($name["type"] == "select")
				{
					print "<td class=\"small_text\"><select class=\"small_text\" name=\"" . $key . "_" . $index . "\" onChange=\"document.myform.what_" . $index . ".value='modify'\">";
					while (list($option_id, $option_title) = each($name["options"]))
					{
						print "<option value=\"" . $option_id . "\">" . $option_title . "</option>" ;
					}
					print "</select></td>\n";
				}
				else if ($name["type"] == "check")
					print "<td class=\"small_text\"><input type=\"checkbox\" class=\"small_text\" name=\"" . $key . "_" . $index . "\" value=\"1\" onChange=\"document.myform.what_" . $index . ".value='modify'\">\n";
				else if ($name["type"] == "date")
					print "<td class=\"small_text\"><input class=\"small_text\" type=\"text\" size=\"" . $tsize . "\" name=\"" . $key . "_" . $index . "\" value=\"\" onChange=\"document.myform.what_" . $index . ".value='modify'\"></td>\n";
			}
			else if ($name != "")
				print "<td class=\"small_text\"><input class=\"small_text\" type=\"text\" size=\"" . $tsize . "\" name=\"" . $key . "_" . $index . "\" value=\"\" onChange=\"document.myform.what_" . $index . ".value='modify'\"></td>\n";
			else
				print "<input type=\"hidden\" name=\"" . $key . "_" . $index . "\" value=\"\">\n";
		}

		$index++;
		print "</tr>\n";
	}

	print "</table>\n";
	
	if ($mode == "edit")
	{
		print "<br><input type=\"hidden\" name=\"index_max\" value=\"" . $index . "\">\n";
		print "<input type=\"hidden\" name=\"orderby\" value=\"" . $orderby . "\">\n";
		print "<input type=\"hidden\" name=\"mode\" value=\"" . $mode . "\">\n";
		print "<center><input type=\"submit\" class=\"page_button\"></center>\n";
		print "</form>\n";
	}
 ?>